# Python 字典
a = {"one": 1, "two": 2, "three": 3}
print("type(a) =", type(a))
print()

## Python 创建字典
### 使用{}创建字典
# 使用字符串作为key
scores = {"数学":95, "英语": 92, "语文": 84}
print("scores =", scores)

# 使用元组和数字作为key
dict1 = {(20, 30): "great", 30: [1, 2, 3]}
print("dict1 =", dict1)

# 创建空元组
dict2 = {}
print("dict2 =", dict2)
print()

### 通过fromkeys()方法创建字典
knowledge = ["语文", "数学", "英语"]
scores = dict.fromkeys(knowledge, 60)
print("scores =", scores)
print()

### 通过dict()映射函数创建字典
a = dict(one = 1, two = 2, three = 3)
print("a =", a)

b = dict([("two", 2), ("one", 1), ("three", 3)])
print("b =", b)

c = dict([["two", 2], ["one", 1], ["three", 3]])
print("c =", c)

d = dict((("two", 2), ("one", 1), ("three", 3)))
print("d =", d)

e = dict((["two", 2], ["one", 1], ["three", 3]))
print("e =", e)

keys = ["one", "two", "three"]
values = [1, 2, 3]
print("keys =", keys)
print("values =", values)
f = dict(zip(keys, values))
print("f =", f)

g = dict()
print("g =", g)
print()

## Python 访问字典
tup = (["two", 26], ["one", 88], ["three", 100], ["four", -59])
dic = dict(tup)
print("dic['one'] =", dic["one"]) # 键存在
#print("dic['five'] =", dic["five"]) # 键不存在

a = dict(two = 0.65, one = 88, three = 100, four = -59)
print("a.get('one') =", a.get("one"))

a = dict(two = 0.65, one = 88, three = 100, four = -59)
print("a.get('five', '该键不存在') =", a.get("five", "该键不存在"))
print()

## Python 删除字典
a = dict(two = 0.65, one = 88, three = 100, four = -59)
print("a =", a)
del a
print("a = ", a)



